﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml;

namespace CSV2XML
{
    class Program
    {
        static void Main(string[] args)
        {
            var thing = "Spells";
            args = new string[3];
            args[0] = @"D:\Code Projects\D20Tools\PF\Documents\CSV\"+thing+".csv";
            args[1] = @"D:\Code Projects\D20Tools\PF\Documents\Templates\"+thing+"Template.xml";
            args[2] = @"D:\Code Projects\D20Tools\PF\Documents\"+thing;


            try
            {
                if (args.Count() >= 2)
                {
                    string inputFileName = args[0];
                    string xmlTemplateFileName = args[1];
                    string destinationPath = args.Count() >= 3 ? args[2] : ".";

                    using (var reader = new StreamReader(inputFileName))
                    {
                        var titleString = reader.ReadLine();
                        var titles = titleString.Split(';');
                        int i = 0;                        
                        while (!new string[] {"name", "Name"}.Contains(titles[i])) i++;
                        int j = 0;
                        while (!new string[] { "source", "Source" }.Contains(titles[j])) j++;

                        string template;
                        using (var templateReader = new StreamReader(xmlTemplateFileName))
                        {
                            template = templateReader.ReadToEnd();                            
                        }

                        while (!reader.EndOfStream)
                        {
                            var line = reader.ReadLine();
                            var items = line.Split(';').Select(s => System.Security.SecurityElement.Escape(s.Replace("'", " ").Replace("\"", " ").Replace(":", " ").Replace("/", " ").Replace(@"&","And"))).ToArray();
                            var name = items[i].Replace(" ", "");
                            var source = items[j].Replace(" ", "");
                            var path = destinationPath + "\\" + source;
                            if (!Directory.Exists(path))
                                Directory.CreateDirectory(path);
                            var outputFileName = path + "\\" + name + ".xml";
                            string outputString = "";
                            try
                            {
                                outputString = String.Format(template, items);
                                XmlDocument doc = new XmlDocument();
                                doc.LoadXml(outputString);
                                doc.Save(outputFileName);
                            }
                            catch (Exception e)
                            {
                                File.WriteAllText( destinationPath + "\\_ERROR"+DateTime.UtcNow.ToString("HHmmssfff")+".err" , outputString +"ERRORMESSAGE:"+ e.Message);
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Please provide an input filename and xmltemplate filename as arguments.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong:" + e.Message);
            }
        }
    }
}
